package com.opennetexam.manager.mapper;

import com.opennetexam.manager.databasemodel.ExamPaperItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 奶茶
 * Created by on 2017/11/28.
 */
@Mapper
public interface ExamPaperItemMapper {

    @Select("SELECT * FROM ExamPaperItem WHERE PaperID = #{paperID} AND ItemID = #{itemID}")
    List<ExamPaperItem> findExamPaperItemBySomeWhere(@Param(value = "paperID") String paperID, @Param(value = "itemID") String itemID);

    @Update("UPDATE ExamPaperItem SET ItemContent = #{itemContent} WHERE PaperID = #{paperID} AND ItemID = #{itemID}")
    Integer updateExamPaperItem(ExamPaperItem examPaperItem);

    @Insert("INSERT INTO ExamPaperItem (\n" +
            "\tPaperID,ItemID,ItemContent\n" +
            ") SELECT #{paperID},#{itemID},#{itemContent}\n" +
            "FROM\n" +
            "\tDUAL\n" +
            "WHERE\n" +
            "\tNOT EXISTS (\n" +
            "\t\tSELECT\n" +
            "\t\t\t1\n" +
            "\t\tFROM\n" +
            "\t\t\tExamPaperItem\n" +
            "\t\tWHERE\n" +
            "\t\t\tItemID = #{itemID}\n" +
            "\t\tAND PaperID = #{paperID}\n" +
            "\t\tLIMIT 1\n" +
            ")")
    Integer insertExamPaperItemIFNotExist(ExamPaperItem examPaperItem);

    @Insert("INSERT INTO ExamPaperItem (PaperID,ItemID,ItemContent) VALUE (#{paperID},#{itemID},#{itemContent}) ON DUPLICATE KEY UPDATE ItemContent = #{itemContent}")
    Integer insertExamPaperItem(ExamPaperItem examPaperItem);

    @Select("SELECT count(1) FROM ExamPaperItem WHERE ItemID = #{itemID} AND PaperID = #{paperID}")
    Integer findExamPaperItemByItemId(@Param(value = "itemID") String itemID,@Param(value = "paperID") String paperID);

    @Delete("DELETE epi FROM ExamPaperItem epi \n" +
            "LEFT JOIN ExamPaper ep ON epi.PaperID = ep.PaperID \n" +
            "WHERE ep.ExamPlanInfoID = #{planId}")
    void delExamPaperItemByPagerIds(@Param("planId") String planId);

}
